
/**
 * @fileoverview A simplistic implementation of a parser coupled to a builder.
 * Some aspects of our complete livecycle is represented here, but this class is
 * simply written in order to create more code so a "big project" feeling is
 * about.
 * Date: 2012/03/25
 * Time: 10:52 AM
 */


goog.provide('lfpug.parser.StoryParser');



/**
 * @constructor
 *
 * @param {lfpug.builder.StoryBuilder} builder The builder associated with this
 * parser.
 */
lfpug.parser.StoryParser = function(builder) {
  this.builder_ = builder;
};


/**
 *
 * @param {Object} data The json I am planning on parsing.
 */
lfpug.parser.StoryParser.prototype.parse = function(data) {
  if (goog.isArrayLike(data)) {
    var l = data.length;
    this.builder_.startBuilder();
    for (var i = 0; i < l; i++) {
      var storyO = data[i];
      //other approaches such as externs could be considered here.
      var id = storyO['id'];
      var story = this.builder_.createStory(id);
      story.setName(storyO['name']);
      story.setDescription(storyO['description']);
      if (goog.isDefAndNotNull(storyO['tasks'])) {
        if (goog.isArrayLike(storyO['tasks'])) {
          var tasks = storyO['tasks'];
          var tl = tasks.length;
          for (var j = 0; j < tl; j++) {
            var taskO = tasks[j];
            var task = this.builder_.createTask(taskO['id']);
            task.setName(taskO['name']);
            task.setDescription(taskO['description']);
            task.setHoursRemaining(taskO['hours_remaining']);
            task.setHoursEstimated(taskO['hours_estimated']);
            this.builder_.commitTask(story, task);
          }
        }
      }
      this.builder_.commitStory(story);
    }
    this.builder_.finishBuilder();
  }

};

